package com.thinkgem.jeesite.common.utils;

import java.security.MessageDigest;

public class MD5 {

	public static String md5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes("UTF-8"));
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (Exception e) {
			e.printStackTrace();

		}
		return str;
	}
	
	public static String md5New(String s) {   
	    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',   
	            'A', 'B', 'C', 'D', 'E', 'F' };   
	    try {   
	        byte[] strTemp = s.getBytes();   
	        MessageDigest mdTemp = MessageDigest.getInstance("MD5");   
	        mdTemp.update(strTemp);   
	        byte[] md = mdTemp.digest();   
			int j = md.length;   
	        char str[] = new char[j * 2];   
	        int k = 0;   
	        for (int i = 0; i < j; i++) {   
	           byte byte0 = md[i];   
	            str[k++] = hexDigits[byte0 >>> 4 & 0xf];   
	            str[k++] = hexDigits[byte0 & 0xf];   
	        }   
	        return new String(str);   
	    } catch (Exception e) {   
	        e.printStackTrace();   
	        return null;   
	    }   
	} 
	public static void main(String[] args) {
		System.out.println(md5("Aa123456"));
		//System.out.println(md5("gx1"));
	}
}
